package com.csht.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.csht.dao.UserRepository;
import com.csht.entity.User;
import com.csht.service.UserService;

@Controller
@SuppressWarnings("unused")
public class UserController {
	@Resource
	private UserService userService;

	@Resource
	private UserRepository dao;


	private final static Log logger = LogFactory.getLog(UserController.class);

	/**
	 * 
	 * @Title: index @Description: ��¼ҳ�� @param: @return @return:
	 *         String @throws
	 */
	@RequestMapping("/")
	public String index() {
		return "user/index";
	}

	/**
	 * 
	 * @Title: login @Description: ��¼ @param: @return @return: String @throws
	 */
	@RequestMapping("/login")
	public String login(String username, String password) {
		User user = null;
		try {

			// user = userService.findByUserNameAndPassword(userName, passWord);

		} catch (Exception e) {
			e.printStackTrace();
		}
		// if (user == null) {
		// return "user/index";
		// } else {
		// return "redirect:/list";
		// }
		return "redirect:/list";
	}

	/**
	 * ��ȡ�û��б�
	 * 
	 */
	@RequestMapping("/list")
	public String list(Model model) {
		List<User> users = userService.getUserList();
		model.addAttribute("users", users);
		return "user/list";
	}

	/**
	 * ��ҳ��ѯ�����û��б�
	 * 
	 */
	@RequestMapping("/listPage")
	public String listPage(Model model, Integer pageNum) {
		int page = 0;
		int size = 5;// ÿҳ��¼����
		page = 1;

		// Sort sort = new Sort(Direction.DESC, "id");
		// Pageable pageable = new PageRequest(page, size, sort);
		Pageable pageable = new PageRequest(page, size);
		Page<User> userpage = userService.findALL(pageable);
		model.addAttribute("pageable", pageable);
		model.addAttribute("userpage", userpage);
		return "user/listPage";
	}

	/**
	 * ��ת���û�ע��ҳ��
	 * 
	 */
	@RequestMapping("/toAdd")
	public String toAdd() {
		return "user/userAdd";
	}

	/**
	 * ��ӱ����û��������б�ҳ��
	 * 
	 */
	@RequestMapping("/add")
	public String add(User user) {
		userService.save(user);
		return "redirect:/listPage";
	}

}
